//
//  AppDelegate.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/11/23.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        setupNavigationTitleColor()
        
        //验证是否加载成功
        print("加载用户信息：\(HXUserAccount.loadUserAccount())")
        
        //创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //创建tabbar
//        let tabbar = HXMainController()
        
//        window?.rootViewController = HXMainController()
        
        window?.rootViewController = defaultController()
        
//        window?.rootViewController?.view.backgroundColor = UIColor.darkGrayColor()
        //显示window
        window?.makeKeyAndVisible()
        
//        print(isNewFeature())
        
        return true
    }
    
    // MARK: - 提供方法,实现不同控制器之间的转换
    private func defaultController() -> UIViewController {
        /// 判断一 --> 用户是否登录(判断是否加载到账号)
        if !HXUserAccount.userLogin {
            return HXMainController()
        }
        /// 判断二 --> 是否是新版本
        return isNewFeature() ? HXNewFeatureViewController() : HXWelcomeViewController()
    }
    
    // MARK: - 实现界面切换 --> 界面切换让Appdelegate去完成,因为这里是程序的入口
    private func switchController(isMain: Bool) {
        window?.rootViewController = isMain ? HXMainController() : HXWelcomeViewController()
    }
    
    // MARK: - 提供一个类方法实现控制器的切换,方便别人调用
    class func switchRootController(isMain: Bool) {
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchController(isMain)
    }
    
    // MARK: - 判断是否是新特性
    private func isNewFeature() -> Bool {
        //获取当前版本
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        //获取上一个版本(存放在偏好设置中)
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        //保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize() // 同步
        
//        /// 测试方法的有效性
//        print("currentVersion:\(currentVersion)")
//        print("sandboxVersion:\(sandboxVersion)")
        
        //比较当前版本和上一个版本
        return currentVersion > sandboxVersion
    }
    
    // MARK: - 其他界面切换控制器,要通过Appdelegate来切换
        // 授权登录后,需要切换两个控制器 --- 欢迎界面,MainViewController
    
    // 设置导航栏的皮肤
    private func setupNavigationTitleColor(){
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

